package com.xxq.semester.controller.exam;


import com.xxq.semester.entity.utils.ResponseEntity;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import com.xxq.semester.service.exam.IExamQuestionsService;
import com.xxq.semester.service.exam.IExamsService;
import com.xxq.semester.entity.utils.ResponseEntity;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController
@RequestMapping("/api/exams")
@Tag(name = "考试管理", description = "创建考试、发布考试等相关操作")
public class ExamsController {
    @Autowired
    private IExamQuestionsService examQuestionsService;

    @Autowired
    private IExamsService examsService;

    /**
     * 创建考试记录
     * @param request 创建考试请求体
     * @return 返回考试ID和提示信息
     */
    @PostMapping("/create")
    @Operation(
        summary = "创建考试",
        description = "创建一个新的考试记录，返回生成的 examId。",
        requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "创建考试请求体，包括教师ID、考试标题、考试描述",
            required = true
        )
    )
    public ResponseEntity<Map<String, Object>> createExam(
            @RequestBody @Parameter(description = "创建考试请求体，包括教师ID、考试标题、考试描述") CreateExamRequest request) {
        Integer examId = examsService.createExam(
                request.getTeacherId(),
                request.getTitle(),
                request.getDescription(),
                request.getStartTime(),
                request.getEndTime(),
                request.getDuration(),
                request.getClassId()
        );

        Map<String, Object> response = new HashMap<>();
        response.put("examId", examId);
//        response.put("message", "考试创建成功");
        return ResponseEntity.success("考试创建成功",response);
    }

    /**
     * 发布考试给学生（修改考试状态为已发布）
     */
    @PostMapping("/{examId}/publish")
    @Operation(summary = "发布考试", description = "将考试状态设置为已发布，学生可以参加，需要指定班级ID列表")
    public ResponseEntity<Map<String, Object>> publishExam(
            @PathVariable @Parameter(description = "考试ID") Integer examId,
            @RequestBody @Parameter(description = "发布考试请求，需包含班级ID列表") PublishExamRequest request) {
        // 支持推送到多个班级
        for (Integer classId : request.getClassIds()) {
            examsService.publishExam(examId, classId);
        }
        Map<String, Object> response = new HashMap<>();
        response.put("examId", examId);
        response.put("classIds", request.getClassIds());
        return ResponseEntity.success("考试发布成功，学生现在可以参加考试",response);
    }

    /**
     * 选择题目并关联到考试
     */


    public static class CreateExamRequest {
        private Integer teacherId;
        private String title;
        private String description;
        private LocalDateTime startTime;
        private LocalDateTime endTime;
        private Long duration;
        private Integer classId;

        // getter和setter
        public Integer getTeacherId() {
            return teacherId;
        }

        public void setTeacherId(Integer teacherId) {
            this.teacherId = teacherId;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public LocalDateTime getStartTime() {
            return startTime;
        }

        public void setStartTime(LocalDateTime startTime) {
            this.startTime = startTime;
        }

        public LocalDateTime getEndTime() {
            return endTime;
        }

        public void setEndTime(LocalDateTime endTime) {
            this.endTime = endTime;
        }

        public Long getDuration() {
            return duration;
        }

        public void setDuration(Long duration) {
            this.duration = duration;
        }

        public Integer getClassId() {
            return classId;
        }

        public void setClassId(Integer classId) {
            this.classId = classId;
        }
    }

    public static class PublishExamRequest {
        private Integer teacherId;
        private List<Integer> classIds;
        // getter和setter
        public Integer getTeacherId() { return teacherId; }
        public void setTeacherId(Integer teacherId) { this.teacherId = teacherId; }
        public List<Integer> getClassIds() { return classIds; }
        public void setClassIds(List<Integer> classIds) { this.classIds = classIds; }
    }

}
